//
//  YWMoreInspectionRecordTableViewController.m
//  maintain
//
//  Created by Kyo on 16/5/31.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMoreInspectionRecordTableViewController.h"
#import "UIColor+CustomColors.h"
#import "YWConst.h"
#import "YWMacro.h"
#import "YWInspectionMissionManager.h"
#import "YWCommonTool.h"
#import "NSDate+Additions.h"
#import "YWMoreRecordTableViewCell.h"
#import "MJRefresh.h"
#import "YWInspectionRecordDetailViewController.h"

@interface YWMoreInspectionRecordTableViewController ()
//@property (nonatomic, strong)UITableView *keepWatchTableView;
@property (nonatomic, strong) NSMutableArray* sourceDatas;
@property (nonatomic, assign)NSInteger page;
@end
@interface YWMoreInspectionRecordTableViewController ()

@end

@implementation YWMoreInspectionRecordTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"inspectionRecords", nil);
    self.view.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.separatorColor = [UIColor customLineColor];
    
    self.page = 1;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTop)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFootor)];
    self.tableView.mj_footer.automaticallyHidden = YES;
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    //有刷新加载时，这句代码要放在刷新下面，不然每次进入界面都是刷新后才有数据
    self.tableView.tableFooterView = [[UIView alloc]init];
}

- (void)refreshTop
{
    self.page = 1;
    [self.tableView.mj_footer resetNoMoreData];
    [self requestData:YWTableViewFreshTypeDown];
}

- (void)refreshFootor
{
    [self requestData:YWTableViewFreshTypeUp];
}

- (void)requestData:(YWTableViewFreshType)refreshType{
    YWWeakSelf(weakSelf);
    if (refreshType == YWTableViewFreshTypeUp) {
        self.page++;
    }
    [[YWInspectionMissionManager shareManager] requestInspectionLineRecordWithPage:self.page pageSize:10 inspectionMissionId:_inspectionMission.inspectionMissionId?:0 inspectionLineId:_inspectionMission.inspectionLine.inspectionLineId?:0 WithComplete:^(BOOL success) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (refreshType == YWTableViewFreshTypeDown) {
                [weakSelf.tableView.mj_header endRefreshing];
            }
            else {
                [weakSelf.tableView.mj_footer endRefreshing];
            }
            weakSelf.sourceDatas = nil;
            [weakSelf.tableView reloadData];
        });
    }];
}

//数据源
-(NSMutableArray *)sourceDatas{
    if (!_sourceDatas) {
        NSMutableArray *sourceData = [NSMutableArray array];
        if (_inspectionMission) {
            [sourceData addObjectsFromArray:[[YWInspectionMissionManager shareManager]inspectionLineRecordWithInspectionMission:_inspectionMission page:self.page]];
        }else{
            [sourceData addObjectsFromArray:[[YWInspectionMissionManager shareManager]inspectionLineRecordWithPage:self.page]];
        }
        //
        if (self.inspectionMission) {
            [[YWInspectionMissionManager shareManager] updateReadStatusWithInspectionMissionId:self.inspectionMission.inspectionMissionId];
        }
        _sourceDatas = sourceData;
    }
    return _sourceDatas;
}

#pragma mark - UITableViewDataSource mothods
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return kLabelH*2 + 10*2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return self.sourceDatas.count;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.sourceDatas[section] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YWMoreRecordTableViewCell *cell = [YWMoreRecordTableViewCell cellWithTableView:tableView];
    YWInspectionLineRecord *lineRecord = self.sourceDatas[indexPath.section][indexPath.row];
    cell.lineRecord = lineRecord;
    return cell;
}

#pragma mark - UITableViewDelegate mothods
-(NSString *)tableViewHeadTxt:(NSInteger)section{
    YWInspectionLineRecord *lineRecord = [self.sourceDatas[section]count]>0?self.sourceDatas[section][0]:nil;
    NSInteger weekDay = [NSDate dateWithTimeIntervalSince1970:lineRecord.startTime/1000].weekday;
    NSString *weekStr = [YWCommonTool weekDayString:weekDay-1];
    NSString *timeString = [NSString stringWithFormat:@"%@ %@",[YWCommonTool showChineseFormatterDate:lineRecord.startTime/1000],weekStr];
    //    YWHandler *patrolMan = PatrollineRecord.patrolMan;
    //    NSString *headText =[NSString stringWithFormat:@"%@  %@",patrolMan.userName,timeString];
    //    headText = [NSString stringWithFormat:@"%@\n%@",self.patrolLine.patrolLineName,headText];
    NSString *headText = timeString;
    return lineRecord!=nil?headText:@"";
}
#pragma mark - UITableViewDataDelegate
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    NSString *headText = [self tableViewHeadTxt:section];
    CGFloat height = [YWCommonTool getHeightOfLabel:headText andWidth:SCREEN_WIDTH - 2 * 15 andFont:KSmallContentFont andMinHeight:kLabelH];
    return height+10;
}

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.sourceDatas.count>0) {
        static NSString* HeaderIdentifier = @"YWMoreInspectionRecordTableViewForHeaderInSection";
        
        UITableViewHeaderFooterView* headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
        if (headerView == nil) {
            headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
            UILabel* label = [[UILabel alloc] init];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor customTextGrayColor];
            label.tag = 1006;
            label.numberOfLines = 0;
            label.font = KSmallContentFont;
            [headerView addSubview:label];
        }
        NSString *headText = [self tableViewHeadTxt:section];
        UILabel* label = [headerView viewWithTag:1006];
        label.text = headText;
        CGFloat height = [YWCommonTool getHeightOfLabel:headText andWidth:SCREEN_WIDTH - 2 * 15 andFont:KSmallContentFont andMinHeight:kLabelH];
        label.frame = CGRectMake(15, 5, SCREEN_WIDTH - 2 * 15, height);
        
        return headerView;
    }
    return nil;
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YWInspectionRecordDetailViewController *detailVC = [[YWInspectionRecordDetailViewController alloc]init];
    YWInspectionLineRecord *lineRecord = self.sourceDatas[indexPath.section][indexPath.row];
    detailVC.lineRecord = lineRecord;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
